package com.it.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.dao.MemberDAO;
import com.it.dao.ProductDAO;
import com.it.dao.RecycleDAO;
import com.it.dao.SysuserDAO;
import com.it.entity.*;
import com.util.Info;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class RecycleController extends BaseController {

	@Resource
    RecycleDAO recycleDAO;
    @Resource
    ProductDAO productDAO;
    @Resource
    MemberDAO memberDAO;
    @Resource
    SysuserDAO sysuserDAO;

	//回收列表
	@ResponseBody
    @RequestMapping("admin/recycleList")
    public HashMap<String,Object> recycleList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,@RequestParam(defaultValue = "1",value = "pageSize") Integer pageSize,HttpServletRequest request){
        Sysuser admin = (Sysuser)request.getSession().getAttribute("admin");
        String key = request.getParameter("key");
        HashMap<String,Object> res = new HashMap<String,Object>();
        HashMap map = new HashMap();
        map.put("key", key);
        List<Recycle> objectlist = recycleDAO.selectAll(map);
        PageHelper.startPage(pageNum, pageSize);
        List<Recycle> list = recycleDAO.selectAll(map);
        for(Recycle recycle:list){
            Member member = memberDAO.findById(recycle.getMemberid());
            recycle.setMember(member);
        }
        PageInfo<Recycle> pageInfo = new PageInfo<Recycle>(list);
        res.put("pageInfo", pageInfo);
        res.put("list", objectlist);

        return res;
    }


    @ResponseBody
    @RequestMapping("recycleLb")
    public HashMap<String,Object> recycleLb(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,@RequestParam(defaultValue = "1",value = "pageSize") Integer pageSize,HttpServletRequest request){
        Member sessionmember = (Member) request.getSession().getAttribute("sessionmember");
        HashMap<String,Object> res = new HashMap<String,Object>();
        HashMap map = new HashMap();
        map.put("memberid", sessionmember.getId());
        List<Recycle> objectlist = recycleDAO.selectAll(map);
        PageHelper.startPage(pageNum, pageSize);
        List<Recycle> list = recycleDAO.selectAll(map);
        PageInfo<Recycle> pageInfo = new PageInfo<Recycle>(list);
        res.put("pageInfo", pageInfo);
        res.put("list", objectlist);
        return res;
    }
		
	
	//添加回收
	@ResponseBody
	@RequestMapping("recycleAdd")
	public HashMap<String,Object> recycleAdd(Recycle recycle ,HttpServletRequest request) {
        Member sessionmember = (Member) request.getSession().getAttribute("sessionmember");
        HashMap<String,Object> res = new HashMap<String,Object>();
        recycle.setMemberid(sessionmember.getId());
        recycle.setStatus("待估价");
        recycle.setSavetime(Info.getDateStr());
        recycleDAO.add(recycle);
		return null;
	}

    @ResponseBody
    @RequestMapping("admin/recycleShow")
    public HashMap<String,Object> recycleShow(int id,HttpServletRequest request) {
        HashMap<String,Object> res = new HashMap<String,Object>();
        Recycle recycle = recycleDAO.findById(id);
        res.put("recycle", recycle);
        return res;
    }

    @ResponseBody
    @RequestMapping("admin/recycleEdit")
    public HashMap<String,Object> recycleEdit(int id,double price,HttpServletRequest request) {
        Recycle recycle = recycleDAO.findById(id);
        recycle.setPrice(price);
        recycle.setStatus("已估价");
        recycleDAO.update(recycle);
        return null;
    }

    @ResponseBody
    @RequestMapping("admin/recycleaddrEdit")
    public HashMap<String,Object> recycleaddrEdit(int id,String addr,HttpServletRequest request) {
        Recycle recycle = recycleDAO.findById(id);
        recycle.setAddr(addr);
        recycleDAO.update(recycle);
        return null;
    }



	
	//删除回收
	@ResponseBody
	@RequestMapping("admin/recycleDel")
	public HashMap<String,Object> recycleDel(int id,HttpServletRequest request) {
		recycleDAO.delete(id);
		return null;
	}

    /**
     * 更新状态
     */
    @ResponseBody
    @RequestMapping("updaterecyclestatus")
    public HashMap<String,Object> updaterecyclestatus(int id,String status,HttpServletRequest request) {
        Recycle recycle = recycleDAO.findById(id);
        recycle.setStatus(status);
        recycleDAO.update(recycle);
        return null;
    }

}
